﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QDT.Common;
using QDT.Core.Experience;
using QDT.MVC.Security;
using QDT.Services.Experience;
using QDT.Web.Code;

namespace QDT.Web.Controllers
{
    public class ExperienceController : BaseController
    {

        public ActionResult Index(int id)
        {
            var peoject = DbProvider.GetByID<ExpProject>(id);
            return View(peoject);
        }

        [HttpPost]
        [DefaultAuthorize]
        public ActionResult Bid(int id, decimal money)
        {
            if (money % 1000 != 0)
            {
                return Error("体验金必须是1000的整数倍！");
            }

            var user = QDTContext.Current.User;

            if (user.GroupID != 3)
            {
                return Error("安全密码错误");
            }
            try
            {
                IoC.Resolve<IExpService>().Bid(user.ID, id, money);
            }
            catch (Exception e)
            {
                return Error(e.Message);
            }

            return RedirectToAction("BidSuccess", "Project", new { projectId = id, loadMoney = money });
        }

        [OutputCache(Duration = 30)]
        public ActionResult BidList(int projectid)
        {
            var credits = DbProvider.D<ExpCredit>().Where(x => x.ExpProjectId == projectid).ToPagedList(0, 20);
            return PartialView(credits.ToList());
        }

        [OutputCache(Duration = 30)]
        public ActionResult AllBidList(int projectid)
        {
            var credits = DbProvider.D<ExpCredit>().Where(x => x.ExpProjectId == projectid).ToList();
            return PartialView(credits);
        }

    }
}
